using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MediaPortal.Configuration;

namespace MP_Syndication.Configuration
{
    public partial class ConfigurationForm : Form
    {
        /// <summary>
        /// Database that holds configuration data
        /// </summary>
        private SyndicationDatabase syndicationDB;

        /// <summary>
        /// List containing all feeds
        /// </summary>
        private List<Feed> _feedsList;

        public ConfigurationForm()
        {
            InitializeComponent();
            _feedsList = new List<Feed>();
        }

        /// <summary>
        /// Load current configuration from database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfigurationForm_Load(object sender, EventArgs e)
        {
            // Load database
            syndicationDB = new SyndicationDatabase();
            syndicationDB.getFeeds(ref _feedsList);
            
            foreach(Feed feed in _feedsList){
                addFeedToListView(feed);
            }
        }

        /// <summary>
        /// Show new feed form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btNew_Click(object sender, EventArgs e)
        {
            new FeedForm(syndicationDB, this).Show();
        }

        // Adds a new feed to the feed list
        public void addFeed(Feed feed)
        {
            _feedsList.Add(feed);
            addFeedToListView(feed);
        }

        private void addFeedToListView(Feed feed)
        {
            string[] items = new String[] { feed.FeedName, feed.TickerEnabled?"Yes":"No", "#Syndication." + feed.Property, feed.FeedUrl };
            feedList.Items.Add(new ListViewItem(items));
        }

        #region panel control

        /// <summary>
        /// Show weather settings panel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btWeather_Click(object sender, EventArgs e)
        {
            // Update visual button state
            btWeather.BackColor = Color.LightSteelBlue;
            btWeather.FlatAppearance.BorderColor = Color.CornflowerBlue;

            btFeeds.BackColor = Color.White;
            btFeeds.FlatAppearance.BorderColor = Color.White;

            // Show/hide panels
            feedsPanel.Left = 69;
            feedsPanel.Top = 56;
            weatherPanel.Left = 69;
            weatherPanel.Top = 56;
            feedsPanel.Visible = false;
            weatherPanel.Visible = true;
        }

        /// <summary>
        /// Show feedsettings panel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btFeeds_Click(object sender, EventArgs e)
        {
            // Update visual button state
            btFeeds.BackColor = Color.LightSteelBlue;
            btFeeds.FlatAppearance.BorderColor = Color.CornflowerBlue;

            btWeather.BackColor = Color.White;
            btWeather.FlatAppearance.BorderColor = Color.White;

            // Show/hide panels
            feedsPanel.Left = 69;
            feedsPanel.Top = 56;
            weatherPanel.Left = 69;
            weatherPanel.Top = 56;
            weatherPanel.Visible = false;
            feedsPanel.Visible = true;
        }

        #endregion

        private void feedList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (feedList.SelectedItems.Count != 0)
            {
                btModify.Enabled = true;
                btRemove.Enabled = true;
            }
            else
            {
                btModify.Enabled = false;
                btRemove.Enabled = false;
            }
        }

        private void btRemove_Click(object sender, EventArgs e)
        {
            // Make sure there is an item selected
            if (feedList.SelectedItems.Count == 0) return;

            int selectedItemnr = feedList.SelectedIndices[0];

            syndicationDB.deleteFeed(_feedsList[selectedItemnr].FeedId);
            _feedsList.RemoveAt(selectedItemnr);
            feedList.Items.RemoveAt(selectedItemnr);
        }
    }
}